import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import io.minio.*;
import io.minio.errors.*;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * Date:2023/8/14
 * Author:丐版小杨哥
 * Description:大文件上传
 */
public class BigFile {
    //实现文件分段存储
    @Test
    public void Chunk() throws Exception {
        //原文件
        String source="D:\\io\\测试io.mp4";
        //写入分区文件
        String chunk="D:\\io\\chunk";
        File chunkFile = new File(chunk);
        if (!chunkFile.exists()){
            chunkFile.mkdir();
        }
        RandomAccessFile r = new RandomAccessFile(source,"r");
        //分区大小
        double regin = 1024 * 1024 *1;
        //获取文件分段数量
        int ceil =(int) Math.ceil(new File(source).length()*1.0/ regin);
        byte [] bytes=new byte[1024];
        for (int i=0;i<ceil;i++){
            //写入文件
            File file = new File(chunk +"//"+ i);
            RandomAccessFile rw = new RandomAccessFile(file,"rw");
            int len;
            while ((len = r.read(bytes))!=-1){
                rw.write(bytes,0,len);
                if (rw.length()>=regin){
                    break;
                }
            }
            rw.close();
        }
        r.close();
    }
    //实现文件合并
    @Test
    public void marge() throws Exception {
        String source="D:\\io\\测试io.mp4";
        String target="D:\\io\\target.mp4";
        //分区文件
        String chunk="D:\\io\\chunk\\";
        RandomAccessFile rw = new RandomAccessFile(new File(target),"rw");
        File[] files = new File(chunk).listFiles();
        List<File> listFiles = Arrays.asList(files);
        byte [] bytes=new byte[1024];
        Collections.sort(listFiles, (o1, o2) -> Integer.parseInt(o1.getName())-Integer.parseInt(o2.getName()));
        for (File file:listFiles) {
            RandomAccessFile r = new RandomAccessFile(file, "r");
            int len=-1;
            while ((len=r.read(bytes))!=-1){
                rw.write(bytes);
            }
            r.close();
        }
        rw.close();
    }
    //上传本地文件
    @Test
    public void uploadChunk() throws Exception {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint("http://8.130.42.231:9000")//ip地址
                        .credentials("myname", "mypassword")//账号密码
                        .build();//构建对象
        for (int i=0;i<8;i++){
            UploadObjectArgs test = UploadObjectArgs.builder()
                    .bucket("mediafiles")//桶名称
                    .filename("D:\\io\\chunk\\"+i)//上传本地文件路径
                    .object("temp/"+i)//上传命名称
                    .build();
            minioClient.uploadObject(test);
        }
    }
    //Minio文件合并
    @Test
    public void minioMager() throws Exception {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint("http://8.130.42.231:9000")//ip地址
                        .credentials("myname", "mypassword")//账号密码
                        .build();
        List<ComposeSource> composeSourceList=new ArrayList<ComposeSource>();
        for (int i=0;i<8;i++){
            ComposeSource source = ComposeSource.builder().bucket("mediafiles").object("temp/"+i).build();
            composeSourceList.add(source);
        }
        ComposeObjectArgs build = ComposeObjectArgs.builder()
                .bucket("mediafiles")
                .object("new.mp4")
                .sources(composeSourceList)
                .build();
        minioClient.composeObject(build);
    }
}